package com.tiro.springboot.sample.common;


/**
 * @author long.qul
 * @date 15/08/2017
 */
public interface GenericEnum {

    /**
     * 代码
     * @return
     */
    int getCode();

    /**
     * 文字标签
     * @return
     */
    String getLabel();

    /**
     * 从代码获得枚举类型
     * @param clz
     * @param code
     * @param <T>
     * @return
     * @throws IllegalArgumentException
     */
    static <T extends Enum<T> & GenericEnum> T fromCode(Class<T> clz, int code) throws IllegalArgumentException {
        T[] enums = clz.getEnumConstants();
        for (int i = 0; i < enums.length; i++) {
            T t = enums[i];
            if (t.getCode() == code) {
                return t;
            }
        }
        throw new IllegalArgumentException("unknonw generic enum");
    }

}
